﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using SMSROUTER.Business;
using System.Web.UI.HtmlControls;
using Utils;
using System.IO;
using Settings;
using System.Data;
namespace SMSROUTER.UI
{
    public partial class RouterManagement : BasePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        private void paging_PagingChange(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            pagingbottom.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            BindGridData();
            pagingtop.Visible = pagingbottom.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                pagingtop.PageDisplay = pagingbottom.PageDisplay = 6;
                BindDropDownData();
                BindGridData();
            }
        }

        private void BindGridData()
        {
            try
            {
                int currentPage = pagingtop.CurrentPage;
                int pageSize = pagingtop.PageSize;
                int totalRecord = 0;
                grvData.DataSource = SmsRouter.Paging(DB, currentPage, pageSize, ref totalRecord, txtSearchRouterName.Text, ddlSearchRouterType.SelectedValue);
                pagingtop.TotalRecord = pagingbottom.TotalRecord = totalRecord;
                pagingtop.Visible = pagingbottom.Visible = pagingtop.TotalPage > 1;
                grvData.DataBind();
                ltrInfo.Text = "Tổng cộng: " + totalRecord;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        private void BindDropDownData()
        {

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
            BindGridData();
        }

        protected void tSortable_length_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                BindGridData();
            }
            catch { }
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int ID = Convert.ToInt32(e.CommandArgument);
                hdfID.Value = ID.ToString();
                switch (e.CommandName)
                {
                    case "Editing":
                        hdfAction.Value = "Editing";
                        SmsRouter router = DB.SmsRouters.FirstOrDefault(p => p.RouterID == ID);
                        if (router != null)
                        {
                            if (!router.Started)
                            {
                                txtRouterName.Text = router.RouterName;
                                txtRouterDescription.Text = router.RouterDescription;
                                txtTypeOfObject.Text = router.TypeOfObject;
                                txtRouterURL.Text = router.RouterURL;
                                txtRouterURL2.Text = router.RouterURL2;
                                ddlRouterType.SelectedValue = router.RouterType;
                                txtPublicKey.Text = router.PublicKey;
                                txtPublicPrivateKey.Text = router.PublicPrivateKey;
                                chkEnabled.Checked = router.Enabled;
                                RegisterStartUpScript("OpenDialog('dialog1',600);");
                            }
                            else
                            {
                                ShowMessageBox("Method đang hoạt động. Bạn không thể sửa.");
                            }
                        }
                        break;
                    case "Deleting":
                        Authorize(Role.Write);
                        SmsRouter deleteObj = DB.SmsRouters.FirstOrDefault(p => p.RouterID == ID);
                        if (deleteObj != null)
                        {
                            if (!deleteObj.Started)
                            {
                                DB.SmsRouters.DeleteOnSubmit(deleteObj);
                                DB.SubmitChanges();
                                InsertChangeLog("Xóa dữ liệu Router");
                                BindGridData();
                            }
                            else
                            {
                                ShowMessageBox("Router đang hoạt động. Bạn không thể xóa");
                            }
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Authorize(Role.Write);
                if (ValidateInfo())
                {
                    SmsRouter router;
                    if (hdfAction.Value == "Editing")
                        router = DB.SmsRouters.FirstOrDefault(p => p.RouterID == int.Parse(hdfID.Value));
                    else
                    {
                        router = new SmsRouter();
                    }
                    if (router != null)
                    {
                        //Gán thuộc tính
                        router.RouterName = txtRouterName.Text.Trim();
                        router.RouterDescription = txtRouterDescription.Text.Trim();
                        router.TypeOfObject = txtTypeOfObject.Text.Trim();
                        router.RouterURL = txtRouterURL.Text.Trim();
                        router.RouterURL2 = txtRouterURL2.Text.Trim();
                        router.RouterType = ddlRouterType.SelectedValue;
                        router.PublicKey = txtPublicKey.Text.Trim();
                        router.PublicPrivateKey = txtPublicPrivateKey.Text.Trim();
                        router.Enabled = chkEnabled.Checked;
                        if (hdfAction.Value == "AddNew")
                        {
                            router.Started = false;
                            DB.SmsRouters.InsertOnSubmit(router);
                        }
                        //save
                        DB.SubmitChanges();
                        //Ghi log tương tác
                        InsertChangeLog(hdfAction.Value == "Editing" ? string.Format("Cập nhật dữ liệu Router, RouterID: {0}", router.RouterID) : string.Format("Thêm mới dữ liệu Router, RouterID: {0}", router.RouterID));
                    }
                }
                BindGridData();
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }
    }
}